
import { defineStore } from 'pinia'

interface TabbarItem {
  navigateName: string
  selectImg: string
  unselectImg: string
  linkUrl: string
}

const useShopTabbarStore = defineStore('shopTabbar', {
  state: () => ({
    tabbarList: [
      {
        navigateName: '商城',
        unselectImg: '/static/tabbar/home.png',
        selectImg: '/static/tabbar/home-s.png',
        linkUrl: '/pages/mall'
      },
      {
        navigateName: '分类',
        unselectImg: '/static/tabbar/category.png',
        selectImg: '/static/tabbar/category-s.png',
        linkUrl: '/pages/tabbar/category'
      },
      {
        navigateName: '购物车',
        unselectImg: '/static/tabbar/cart.png',
        selectImg: '/static/tabbar/cart-s.png',
        linkUrl: '/pages/tabbar/cart'
      },
      {
        navigateName: '个人中心',
        unselectImg: '/static/tabbar/mine.png',
        selectImg: '/static/tabbar/mine-s.png',
        linkUrl: '/pages/tabbar/shopMine'
      }
    ] as TabbarItem[], // 初始为空，动态生成
    activeTab: 0, // 当前激活的 tab 索引
    activeColor: '#ff3c2a',
    unactiveColor: '#333'
  }),
  actions: {
    /**
     * 切换 TabBar
     * @param index 索引
     */
    tabSwitch(index: number) {
      if (index === this.activeTab) {
        return
      }
      this.activeTab = index
      const target = this.tabbarList[index]

      if (target) {
        uni.switchTab({ url: target.linkUrl })
      }
    },
    setActiveTab(index: number) {
      this.activeTab = index
    }
  }
})

export default useShopTabbarStore
